package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-05-22
 * Time: 22:16
 */
//线程状态
public class Demo13 {
    //查看所有线程状态
    public static void main1(String[] args) {
        for (Thread.State state : Thread.State.values()) {
            System.out.println(state);
        }
    }

    public static void main2(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println("hello thread");
        });

        System.out.println(thread.getState());//NEW是new了thread对象 还没start
        thread.start();
        Thread.sleep(1000);
        System.out.println(thread.getState());//TERMINATED这里开启的线程与内核的线程一一对应 内核的线程已经结束但是Thread对象还在
    }

    public static void main3(String[] args) throws InterruptedException {
        Thread thread2 = new Thread(() -> {
            while (true) {
                //啥也不做
            }
        });

        thread2.start();
        Thread.sleep(1000);
        System.out.println(thread2.getState());//runnable表示就绪状态
        //1.线程正在cpu运行
        //2.线程随时都可以去cpu上执行
    }

    public static void main4(String[] args) throws InterruptedException {
        Thread thread3 = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }) ;

        thread3.start();

        Thread.sleep(1000);
        System.out.println(thread3.getState());//TIMED_WAITING指定时间阻塞
        // 线程阻塞(不参与cpu的调度，不继续执行了) 阻塞的时间由上限
    }

    public static void main5(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();

        thread.join(6000 * 1000);//此时main线程是TIMED_WAITING状态的 线程阻塞
        //必须是join(时间) 没有参数不行就是另一种WAITING情况
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (true) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();

        thread.join();//死等 没有超时间等待
        //状态WAITING

    }
    //block后面
}
